﻿package com.controller;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.entity.User;
import com.service.UserService;
@Controller
@RequestMapping("/user")
public class UserController {
	Logger logger=Logger.getLogger(UserController.class);
	@Resource
	UserService userService;
	/**
	 * web页面用户+密码登录
	 * @param request
	 * @param map
	 * @param session
	 * @return
	 */
	int i=0; 
	@ResponseBody
	@RequestMapping("/login")
	public String login(HttpServletRequest request,ModelMap map,HttpSession session){
		logger.info("登录");
		String user=request.getParameter("user");
		String pwd=request.getParameter("password");
		Map<String,Object> parms=new HashMap<String, Object>();
		parms.put("yusername", user);
		parms.put("ypassword", pwd);
		User users=userService.selectAll(parms);
		int ytrue =1;
		if(users!=null){
			logger.info("用户名"+users.getYusername()+"密码\t"+users.getYpassword());
			session.setAttribute("result", ytrue);
			String str=String.valueOf(users.getYid());
			//return "redirect:/pc/selectByPage?result=1&id="+users.getYid()+"&pageCurrent="+0;
			return str;
		}else{
			return "fail";
		}
	}
	/**
	 * 密码修改
	 */
	@ResponseBody
	@RequestMapping("/updatepassword")
	public String updatepassword(HttpServletRequest request){
		String user=request.getParameter("user");
		String password1=request.getParameter("password1");
		String password2=request.getParameter("password2");
		System.out.println("用户名"+user+"\t密码"+password1+"\t确认密码"+password2);
		
		User users=userService.selectUserName(user);
		if(users!=null){
			System.out.println("用户名存在");
			Map<String,Object> maps=new HashMap<String,Object>();
			maps.put("ypassword", password1);
			maps.put("yid", users.getYid());
			userService.updatepassword(maps);
			return "0";
		}else{
			System.out.println("用户名不存在");
		}
		return "1";
	}
}
